import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train_exam_appro.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import 'model/train_model.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/2/18.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///培训考试合格审核
class TrainExamApproPage extends StatefulWidget {
  final BeanTrainExamAppro beanTrainExamAppro;

  const TrainExamApproPage({Key key, this.beanTrainExamAppro})
      : super(key: key);

  @override
  _TrainExamApproPageState createState() => _TrainExamApproPageState();
}

class _TrainExamApproPageState extends State<TrainExamApproPage>
    with TrainModel {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("合格审核详情"),
      ),
      body: Container(
          child: RefreshIndicator(
        onRefresh: () async {
          await getTrainExamApproDetails(widget.beanTrainExamAppro.trainId);
          ToastUtils.showToast("已刷新");
        },
        child: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(12),
            child: buildColumn(),
          ),
        ),
      )),
    );
  }

  Column buildColumn() {
    return Column(
      children: [
        buildItem("提交审批", 1),
        buildItem("审批结果", 2),
        //buildItem("合格证书", 3),
      ],
    );
  }

  buildItem(String title, int index) {
    Color titleNumberColor = Colors.white;
    Color titleNumberBgColor = Colors.green;

    //审批状态 0待审批  1通过 2拒绝 3 证书下发 4删除
    int statues = widget.beanTrainExamAppro.approStatues;

    if (index == 2) {
      if (statues == 0) {
        titleNumberBgColor = Colors.grey;
        title = "待审批";
      } else if (statues == 1) {
        title = "已审批";
      } else if (statues == 2) {
        title = "已审批";
        titleNumberBgColor = Colors.redAccent;
      }
    } else if (index == 3) {
      if (statues != 3) {
        titleNumberBgColor = Colors.grey;
        title = "证书";
      }
    }

    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          width: 100,
          padding: EdgeInsets.only(top: 4),
          child: Stack(
            children: [
              Container(
                width: 98,
                height: 26,
                padding: EdgeInsets.only(right: 10),
                alignment: Alignment.centerRight,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(20))),
                child: Text(
                  title,
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 14,
                    color: titleNumberBgColor,
                  ),
                ),
              ),
              Container(
                width: 26,
                height: 26,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: titleNumberBgColor,
                    border: Border.all(color: Colors.white, width: 2),
                    borderRadius: BorderRadius.all(Radius.circular(20))),
                child: Text(
                  "$index",
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 18,
                    color: titleNumberColor,
                  ),
                ),
              ),
            ],
          ),
        ),
        Expanded(
          child: buildRightWidget(index),
        ),
      ],
    );
  }

  buildRightWidget(int index) {
    //审批状态 0待审批  1通过 2拒绝 3 证书下发 4删除
    int statues = widget.beanTrainExamAppro.approStatues;

    if (index == 1) {
      int answerTime = widget.beanTrainExamAppro.examAnswerTime;
      int score = widget.beanTrainExamAppro.examObtainScore ~/ 10;

      int min = answerTime ~/ 60;
      int second = (answerTime % 60);
      String minStr = min > 9 ? "$min分" : "0$min分";
      String secondStr = second > 9 ? "$second秒" : "0$second秒";

      return Card(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("提交时间：${widget.beanTrainExamAppro.createTime}"),
              Text("考试时间：${widget.beanTrainExamAppro.examCreateTime}"),
              Text("考试用时：$minStr$secondStr"),
              Text("考试得分：$score 分"),
              Text("培训课时：${widget.beanTrainExamAppro.studyCoureNumber} 课时"),
              Text("签到次数：${widget.beanTrainExamAppro.signNumber} 次"),
            ],
          ),
        ),
      );
    } else if (index == 2) {
      String approTime = "暂无";
      String approRemark = "暂无";

      Color textColor = Colors.grey;
      //审批状态 0待审批  1通过 2拒绝 3 证书下发 4删除
      if (statues == 1 || statues == 2 || statues == 3) {
        approTime = widget.beanTrainExamAppro.approTime;
        approRemark = widget.beanTrainExamAppro.approRemark;
        textColor = Colors.black;
      }
      String approResult = "暂无";
      if (statues == 0) {
        approResult = "待审批";
      } else if (statues == 1 || statues == 3) {
        approResult = "合格";
      } else if (statues == 2) {
        approResult = "不合格";
      } else {
        approResult = "--";
      }
      return Card(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "审批结果：$approResult",
                style: TextStyle(color: textColor),
              ),
              Text(
                "审批时间：$approTime",
                style: TextStyle(color: textColor),
              ),
              Text(
                "审批意见：$approRemark",
                style: TextStyle(color: textColor),
              ),
            ],
          ),
        ),
      );
    }
    Color textColor = Colors.grey;
    String approBianHao = "未下发";
    if (statues == 1 || statues == 3) {
      approBianHao = widget.beanTrainExamAppro.approBianHao;
      textColor = Colors.black;
    }
    return Card(
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "证书编号：$approBianHao",
              style: TextStyle(color: textColor),
            ),
          ],
        ),
      ),
    );
  }
}
